package com.ronglian.register_mini_program.service.impl;

import com.ronglian.register_mini_program.dao.RegisterActivityMapper;
import com.ronglian.register_mini_program.dao.RegisterOrderMapper;
import com.ronglian.register_mini_program.dao.RegisterUserMapper;
import com.ronglian.register_mini_program.model.*;
import com.ronglian.register_mini_program.model.web.RegisterBody;
import com.ronglian.register_mini_program.service.RegisterService;
import com.ronglian.register_mini_program.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("RegisterService")
public class RegisterServiceImpl implements RegisterService {
    /**
     * 表单提交数据
     *
     * @param registerBody
     * @return
     */
    @Autowired
    RegisterActivityMapper activityMapper;
    @Autowired
    RegisterOrderMapper orderMapper;
    @Autowired
    RegisterUserMapper userMapper;

    @Override
    public int insertOrder(RegisterBody registerBody) {
        RegisterUserExample userExample = new RegisterUserExample();
        RegisterUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpen_idEqualTo(registerBody.getOpen_id());
        List<RegisterUser> registerUserList = userMapper.selectByExample(userExample);
        RegisterUser user = null;
        if (registerUserList == null) {
            user = new RegisterUser();
            setValueWithUser(registerBody, user);
            userMapper.insert(user);
        }


        RegisterOrder order = new RegisterOrder();
        order.setIs_payment(1);
//        order.setArrange(registerBody.getArrange());
        order.setActivity_id(registerBody.getActivity_id());
        order.setOpen_id(registerBody.getOpen_id());
        order.setCreate_time(new Date());
        int b = orderMapper.insert(order);
        Integer activity_id = registerBody.getActivity_id();
        if (activity_id != null) {
            RegisterActivityWithBLOBs registerActivityWithBLOBs = activityMapper.selectByPrimaryKey(activity_id);
            if (registerActivityWithBLOBs != null) {
                Integer count = registerActivityWithBLOBs.getCount();
                count += 1;
                registerActivityWithBLOBs.setCount(count);
                activityMapper.updateByPrimaryKeyWithBLOBs(registerActivityWithBLOBs);
            }

        }
        return b;

    }

    private void setValueWithUser(RegisterBody registerBody, RegisterUser user) {
        user.setAge(registerBody.getAge());
        user.setCity(registerBody.getCity());
        user.setClass_grade(registerBody.getClass_grade());
        user.setCompany(registerBody.getCompany());
        if (registerBody.getCreate_time() != null) {
            user.setCreate_time(DateUtil.strToDate(registerBody.getCreate_time()));
        } else {
            user.setCreate_time(new Date());
        }

        user.setDepartment(registerBody.getDepartment());
        user.setEmail(registerBody.getEmail());
        user.setHead_image_url(registerBody.getHead_image_url());
        user.setHome_address(registerBody.getHome_address());
        user.setJob_id(registerBody.getJob_id());
        user.setMark(registerBody.getMark());
        user.setName(registerBody.getName());
        user.setNickname(registerBody.getNickname());
        user.setOpen_id(registerBody.getOpen_id());
        user.setProvince(registerBody.getProvince());
        user.setQq_number(registerBody.getQq_number());
        user.setSchool(registerBody.getSchool());
        user.setSex(registerBody.getSex());
        user.setCountry(registerBody.getCountry());
        user.setStudent_id(registerBody.getStudent_id());
        user.setTelephone(registerBody.getTelephone());
        user.setWechat_number(registerBody.getWechat_number());
        user.setWork_position(registerBody.getWork_position());
    }

    @Override
    public int deleteOrder(String openid, Integer activity_id) {
        RegisterOrderExample example = new RegisterOrderExample();
        RegisterOrderExample.Criteria cri = example.createCriteria();
        cri.andOpen_idEqualTo(openid).andActivity_idEqualTo(activity_id);
        orderMapper.deleteByExample(example);
        //删除订单，用户表信息不修改，order删除 该活动数量减1
        RegisterActivityWithBLOBs registerActivityWithBLOBs = activityMapper.selectByPrimaryKey(activity_id);
        if (registerActivityWithBLOBs != null) {
            Integer count = registerActivityWithBLOBs.getCount();
            if (count > 0) {
                count -= 1;
                registerActivityWithBLOBs.setCount(count);

            }
        }
        return activityMapper.updateByPrimaryKeyWithBLOBs(registerActivityWithBLOBs);
    }
}
